<?php


namespace app\admin\model\base;


use app\BaseModel;

class Special extends BaseModel
{
    /**
     * 获取用户列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @param string $field
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = '', string $field = '*'): array
    {
        //查询
        $data = self::field($field)
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }

    /**
     * 关联题目
     * @return \think\model\relation\HasOne
     */
    public function children(){
        return $this->hasMany(Special::class,'pid','id')->where(['is_del'=>0])->with(['children'])->field('name as label,id,pid,level');
    }

    /**
     * 关联单个数据
     * @return \think\model\relation\HasOne
     */
    public function info(){
        return $this->hasOne(Special::class,'id','pid')->where(['is_del'=>0])->with(['info'])->field('name,id,pid,level');
    }

    /**
     * 获取专业的所有包括子类
     */
    public static function lists(){
        $data = self::where('level','=',1)
            ->with(['children'])
            ->where('is_del','=',0)
            ->order('sort','asc')
            ->field('name as label,id,pid,level')
            ->select()
            ->toArray();
        return $data;
    }
}